// page/component/details/details.js
const db = wx.cloud.database()
let app = getApp()
import orderNum from '../../../utool/rander'
import {formatTime} from '../../../utool/famtime'
Page({
  data:{
    goods: {},
    goodsDetailsList:{},
    cartitem:{},
    num: 1,
    totalNum: 0,
    hasCarts: false,
    curIndex: 0,
    show: false,
    scaleCart: false,
    selected:true,
    cart:[]
    
  },
  onLoad(options) {
    this.getGoodsDetails(options.id)
    wx.getStorage({
      key: 'cart',
      success: (res) => {
        this.setData({
          cart: res.data,
        })
      }
    })
  },
  getGoodsDetails(id) {
    db.collection("goods_details").where({ goods_id: id }).get({
      success: (res) => {
        this.setData({
          goodsDetailsList: res.data[0]
        })
      }
    })
  },
  goCartPage(option) {
    this.setData({
      cartitem: {
        id: option.currentTarget.dataset.goods_id,
        num: option.currentTarget.dataset.num,
        image: option.currentTarget.dataset.goods_image_url,
        totalNum: option.currentTarget.dataset.totalnum,
        price: option.currentTarget.dataset.price,
        title: option.currentTarget.dataset.goods_name,
        selected: option.currentTarget.dataset.selected,
        orderNumber:orderNum.orderNumber(),
        orderTime: formatTime(new Date())
      },

    })
    // console.log(this.data.cartitem.orderNumber)
    this.data.cart.push(this.data.cartitem)
    wx.setStorage({
      key: "cart",
      data: this.data.cart
    })
    // app.globalData.id=option.currentTarget.dataset.goods_id
    // app.globalData.num=option.currentTarget.dataset.num
    // app.globalData.totalNum=option.currentTarget.dataset.totalnum
    // app.globalData.price=option.currentTarget.dataset.price
    // app.globalData.image=option.currentTarget.dataset.goods_image_url
    // app.globalData.title=option.currentTarget.dataset.goods_name
    // app.globalData.selected=option.currentTarget.dataset.selected

    wx.switchTab({
      url: '../cart/cart'
      //?goods_id='+app.globalData.goods_id+"&num="+num+"&totalnum="+totalnum+"&price="+price,

    })
  },

  addCount() {
    let num = this.data.num;
    num++;
    this.setData({
      num : num
    })
  },

  addToCart() {
    const self = this;
    const num = this.data.num;
    let total = this.data.totalNum;

    self.setData({
      show: true
    })
    setTimeout( function() {
      self.setData({
        show: false,
        scaleCart : true
      })
      setTimeout( function() {
        self.setData({
          scaleCart: false,
          hasCarts : true,
          totalNum: num + total
        })
      }, 200)
    }, 300)
  },

  bindTap(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      curIndex: index
    })
  }
})